package com.example.zheng.qq.util;

import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.widget.ImageView;

import com.example.zheng.qq.R;
import com.example.zheng.qq.controller.CreateTable;
import com.example.zheng.qq.view.Circle1;

import java.net.URL;

/**
 * Created by zheng on 17-4-27.
 */
public class Util {
    public static int imageId= R.drawable.qq;
    public static Uri imageUri=Uri.parse("android.resource://com.example.zheng.qq/drawable/qq");
    //创建数据库USER.db
    public static SQLiteDatabase Db=new CreateTable(ContextApplication.getContext(),"USER.db",null,1).getWritableDatabase();
    public static void SqlPictureId(String uname, ImageView imageView){
        try {
            Cursor data=Db.rawQuery("select * from user where uname=?",new String[]{uname});
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 4;//图片宽高都为原来的四分之一，即图片为原来的八分之一
            Bitmap bitmap=null;
            if(data.moveToNext()) {
                String id = data.getString(2);
                 bitmap = BitmapFactory.decodeStream(ContextApplication.getContext().getContentResolver().openInputStream(Uri.parse(id)),null,options);
                data.close();
            }else{
                bitmap = BitmapFactory.decodeResource(ContextApplication.getContext().getResources(),R.drawable.qq);

            }
            Bitmap b = Circle1.createCircle(bitmap);
            imageView.setImageBitmap(b);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
